package com.yhny.lykj.yuhannongye.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.model.AddressListModel;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.Tools;

public class AddAddressActivity extends BaseActivity {
    private RadioButton normalRb;
    private EditText infoEt;
    private EditText phoneEt;
    private EditText nameEt;
    private TextView rightTv;
    private String aid;
    private int fromType;
    private AddressListModel.ListEntity address = new AddressListModel.ListEntity();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        fromType = getIntent().getIntExtra(ADDRESS_VIEW,0);

        findViewById(R.id.left_text).setOnClickListener(this);
        findViewById(R.id.left_group).setOnClickListener(this);
        TextView topName = (TextView)findViewById(R.id.name_top_bar);
        rightTv = (TextView)findViewById(R.id.right_text);
        rightTv.setOnClickListener(this);
        rightTv.setText("保存");
        rightTv.setVisibility(View.VISIBLE);
        findViewById(R.id.right_group).setOnClickListener(this);

        normalRb = (RadioButton)findViewById(R.id.address_normal);//设置默认
        infoEt = (EditText)findViewById(R.id.address_info);//详细地址
        phoneEt = (EditText)findViewById(R.id.address_phone);//电话
        nameEt = (EditText)findViewById(R.id.address_name);//姓名
        if (fromType==ADDRESS_NEW){
            topName.setText("新增收货地址");
        }else {
            address= (AddressListModel.ListEntity )getIntent().getSerializableExtra(ADDRESS_ID);
            topName.setText("编辑收货地址");
            if (address.isdefault==1){
                normalRb.setChecked(true);
            }else{
                normalRb.setChecked(false);
            }
            infoEt.setText(address.adress);
            phoneEt.setText(address.tel);
            nameEt.setText(address.name);
        }
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_0:{
                BaseModel baseModel = (BaseModel)model;
                if (!isDataEmpty(baseModel)&&baseModel.status==NET_SUSSEC){
                    if (fromType==ADDRESS_NEW){
                        LSCToast.show(this,"添加成功");
                    }else{
                        LSCToast.show(this,"修改成功");
                    }

                }else{
                    if (fromType==ADDRESS_NEW){
                        LSCToast.show(this,"添加失败");
                    }else{
                        LSCToast.show(this,"修改失败");
                    }

                }
                break;
            }
        }
        dismissProgressDialog();
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_group:
            case R.id.left_text:{
                finish();
                break;
            }
            case R.id.right_group:
            case R.id.right_text:{
                //保存
                int isNormal = 1;
                if (normalRb.isChecked()){
                    isNormal = 1;
                }else{
                    isNormal = 0;
                }
                String name = nameEt.getText().toString();
                String addresstext = infoEt.getText().toString();
                String phone = phoneEt.getText().toString();

                if (name.isEmpty()){
                    LSCToast.show(this,"请输入姓名!");
                    return;
                }
                if (addresstext.isEmpty()){
                    LSCToast.show(this,"请输详细地址!");
                    return;
                }
                if (phone.isEmpty()){
                    LSCToast.show(this,"请输联系电话!");
                    return;
                }
                if (!Tools.isMobilePhoneNumber(phone)){
                    LSCToast.show(this,"请输入正确的手机号!");
                    return;
                }

                if (fromType==ADDRESS_NEW){
                    DataManager.getData(MODEL_PROCESS_0, NetHelper.User.addAddress(name,addresstext,phone), this);
                }else{
                    DataManager.getData(MODEL_PROCESS_0, NetHelper.User.modifyAddress(address.addressid,name,addresstext,phone,""+isNormal), this);
                }
                finish();
                break;
            }

        }
        super.onClick(v);
    }



}
